package main;

import clases.*;
import clases.Vectores.Funcion;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;

/**
  *  4g3nt
  *  Copyright (C) 2008  Carlos Molinero y César Andrés
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *    You should have received a copy of the GNU General Public License
  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
public class Global {
	static FileWriter fichero = null;
	static PrintWriter pw = null;
        static Mundo _mundo ;
	static Hashtable _celulas = new Hashtable();
	static boolean _error = false;
	static Hashtable _funcionesCreaccion= new Hashtable();
	static Hashtable _funcionesDestruccion= new Hashtable();
	static Hashtable _funcionesUtilidad= new Hashtable();
	static long _tiempoGlobal= System.currentTimeMillis();
	static ArrayList _threads = new ArrayList(); 
	@SuppressWarnings("unchecked")
	public static void setThread(Thread thread)
	{
		_threads.add(thread);
	}
	public static void ejecutaThreads()
	{
		for (int i = 0 ; i < _threads.size();i++)
		{
			Thread var_thread = (Thread) _threads.get(i);
			var_thread.start();
		}
	}
	@SuppressWarnings("deprecation")
	public static void matarThreads()
	{
		for (int i = 0; i < _threads.size();i++)
		{
			Thread var_thread = (Thread) _threads.get(i);
			var_thread.stop();
			
		}
	}
	public static void inicializa()
	{
		_mundo = new Mundo();
		Celula celula = new CelulaNula();
		celula.setAgente(new AgenteNulo());
		_celulas.put("NULL", celula);
		_tiempoGlobal = System.currentTimeMillis();
	}
	public static void addFuncionCreaccion(Funcion funcion)
	{
                String nick = funcion.getNick();
		if (_funcionesCreaccion.get(nick)!=null) Errores.insertar("Nick duplicado funcion creaccion "+nick);
		_funcionesCreaccion.put(nick, funcion);
	}
	public static void addFuncionDestruccion(Funcion funcion)
	{
                String nick = funcion.getNick();
        	if (_funcionesDestruccion.get(nick)!=null) Errores.insertar("Nick duplicado funcion destruccion "+nick);
		_funcionesDestruccion.put(nick, funcion);
	}
	public static void addFuncionUtilidad(Funcion funcion)
	{
            String nick = funcion.getNick();
            if (_funcionesUtilidad.get(nick)!=null) Errores.insertar("Nick duplicado funcion utilidad "+nick);
		_funcionesUtilidad.put(nick, funcion);
	}
	public static void setCelula(Celula celula)
	{
		if(_celulas.get(celula.getID())!=null)Errores.insertar("Duplicacion del nick de la celula "+celula.getID());
		
		_celulas.put(celula.getID(), celula);
	}
	public static Celula getCelula(String key)
	{
		return (Celula)_celulas.get(key);
	}
	public static Funcion getFCreaccion(String key)
	{
		return (Funcion)_funcionesCreaccion.get(key);
	}
	public static Funcion getFDestruccion(String key)
	{
		return (Funcion)_funcionesDestruccion.get(key);
	}
	
	public static Funcion getFUtilidad(String key)
	{
		return (Funcion)_funcionesUtilidad.get(key);
	}
	public static Mundo getMundo()
	{
		return _mundo;
	}
	
	public static synchronized long getTiempo()
	{
		
		long tiempo = System.currentTimeMillis();
		long devolver = tiempo-_tiempoGlobal;
		_tiempoGlobal = tiempo;
		return devolver;
	}
	public static void printMundo()
	{
		_mundo.print();
		Errores.escribir();
	}
	
	public static void inicializa(String url) throws IOException
	{
		Global.inicializa();
        fichero = new FileWriter(url);
        pw = new PrintWriter(fichero);
        pw.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
        pw.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        pw.println("<head>");
        pw.println("	<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\"/>");
        pw.println("	<title>Agentes</title>");
        pw.println("	<link rel=\"stylesheet\" href=\"index_files/jquery.treeview.css\" />");
        pw.println("<link rel=\"stylesheet\" href=\"index_files/screen.css\" />");
        pw.println("<link rel=\"stylesheet\" href=\"index_files/tables.css\" />");
        pw.println("	<script src=\"index_files/jquery.js\" type=\"text/javascript\"></script>");
        pw.println("	<script src=\"index_files/jquery.cookie.js\" type=\"text/javascript\"></script>");
        pw.println("	<script src=\"index_files/jquery.treeview.js\" type=\"text/javascript\"></script>");
        pw.println("	<script type=\"text/javascript\">");
        pw.println("	$(document).ready(function(){");
        pw.println("		$(\"#browser\").treeview({");
        pw.println("			toggle: function() {");
        pw.println("				console.log(\"%s was toggled.\", $(this).find(\">span\").text());");
        pw.println("			}");
        pw.println("		});");
        pw.println("		$(\"#add\").click(function() {");
        pw.println("			var branches = $(\"<li><span class='folder'>New Sublist</span><ul>\" + ");
        pw.println("				\"<li><span class='file'>Item1</span></li>\" +"); 
        pw.println("				\"<li><span class='file'>Item2</span></li></ul></li>\").appendTo(\"#browser\");");
        pw.println("			$(\"#browser\").treeview({");
        pw.println("				add: branches");
        pw.println("			});");
        pw.println("		});");
        pw.println("	});");
        pw.println("	</script>");
        pw.println("	</head>");
        pw.println("	<body>");
        pw.println("	<div id=\"main\">");
        pw.println("	Agentes:");
        pw.println("	<ul id=\"browser\" class=\"filetree treeview-famfamfam\">");
		
	}
	public static void escribir( String cadena) 
	 {
		synchronized(pw){
	        pw.println(cadena);}
	 }
	public static void setError()
	{
		_error = true;
	}
	public boolean getError()
	{
		return _error;
	}
	
	 public static void cerrar() throws IOException
	 {
		 pw.println("	</ul>");
		 pw.println("	</div>");
		pw.println("	</body></html>");
         if (null != fichero)
             fichero.close();		 
	 }
         public static void printFuncion(Funcion funcion, String nombre, String infoError)
         {
             if (funcion==null)
                {
                Errores.insertar("No se ha definido una " + nombre +" " +infoError);
                }
             else
             {
                         Global.escribir("<li class=\"closed\"><span class=\"folder\">"+nombre+":</span>");
			 Global.escribir("<ul>");
                         funcion.print();
			 Global.escribir("</ul>");
			 Global.escribir("</li>");
             }
         }
}